package com.tan.config;


import com.alibaba.fastjson.JSONArray;
import com.tan.domain.ProvinceCityDistrict;
import com.tan.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author tlj
 * @date 2022/08/19 10:23:09
 */
@Component
@Slf4j
public class InitData implements CommandLineRunner {
    final String filePath = "ProvinceCityDistrict.json";
    final String ymlPath = "application.yml";

    @Override
    public void run(String... args) throws Exception {
        loadProvince();
        // ClassPathResource classPathResource = new ClassPathResource("application.yml");
        // System.out.println(IOUtils.toString(classPathResource.getInputStream(), StandardCharsets.UTF_8));
    }

    public void loadProvince()  {
        ClassPathResource classPathResource = new ClassPathResource(filePath);
        System.out.println("获取文件路径：fileName="+classPathResource);
        String s = null;
        try {
            s = IOUtils.toString(classPathResource.getInputStream(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            log.info("省市区信息加载异常：{}", e.getMessage());
        }
        UserServiceImpl.provinceCityDistricts = JSONArray.parseArray(s, ProvinceCityDistrict.class);
        System.out.println("加载省市区数据完成");
    }
}
